﻿#include "formitemwidgetgroup.h"
#include "ui_formitemwidgetgroup.h"
#include <QMouseEvent>
#include <QDebug>

FormItemWidgetGroup::FormItemWidgetGroup(QStringList titlelst, QWidget *parent) :
    QWidget(parent),IsEnableUse(true),IsLoadSubItem(false),
    ui(new Ui::FormItemWidgetGroup)
{
    ui->setupUi(this);

    ui->lblTitleName->setText(titlelst.first() + QStringLiteral(":"));
    ui->lblTitleText->setText(titlelst.last());

    ui->lbl_putstart->setText(QStringLiteral("开始时间"));
    ui->lbl_putstart->setMinimumWidth(50);
    ui->timeEdit_start->setTime(QTime(
                                    QDateTime::currentDateTime().toString("hh").toInt(),
                                    QDateTime::currentDateTime().toString("mm").toInt()+ 5));
    ui->timeEdit_start->setWrapping(true);
    //should load from the last put value

    ui->ckb_ignorerror->setChecked(true);

    ui->lbl_cycle->setText(QStringLiteral("周期"));
    ui->timeEdit_cycle->setTime(QTime(0,5));//just like as the start timeedit.
    ui->timeEdit_cycle->setWrapping(true);

    ui->lbl_putCount->setText(QStringLiteral("投放次数"));
    ui->lineEdit_count->setText("1");//default.

//    ui->lbl_status->setText(QStringLiteral("状态:"));

    connect(ui->chkbox,&QCheckBox::stateChanged,[=](int state){
        ui->lblTitleName->setEnabled(state);
        ui->lblTitleText->setEnabled(state);
        ui->lblValuename->setEnabled(state);
        ui->lblvalueText->setEnabled(state);
        ui->lbl_putCount->setEnabled(state);
        ui->lineEdit_count->setEnabled(state);
//        ui->lbl_dlytime->setEnabled(state);
//        ui->dly_timeEdit->setEnabled(state);
        ui->lbl_status->setEnabled(state);
//        ui->btn_status->setEnabled(state);

        ui->lbl_putstart->setEnabled(state);
        ui->timeEdit_start->setEnabled(state);
        ui->lbl_cycle->setEnabled(state);
        ui->timeEdit_cycle->setEnabled(state);
        //new one->button of start put.
        ui->btn_startPut->setEnabled(state);

        ui->lbl_ignorError->setEnabled(state);
        ui->ckb_ignorerror->setEnabled(state);

        IsEnableUse = state;
        m_itemData.setEnable(state);
        emit sig_EnableItem(state);
    });

    connect(ui->ckb_ignorerror,&QCheckBox::stateChanged,[=](int state){
        ui->lbl_ignorError->setEnabled(state);
        m_itemData.setIgnorError(state);
    });

    //preview
    QString str_ckbstyle = QString("QCheckBox#%1::indicator {"
                                    "width: 40px;height: 25px;}"
                                   "QCheckBox#%1::indicator:unchecked {"
                                   "image: url(:/res/images/ckbox_off.png);}"
                                   "QCheckBox#%1::indicator:checked {"
                                    "image: url(:/res/images/ckbox_on.png);}"
                );
    QString str_lineEdit_count = QString("QLineEdit{background:darkGray;border:transparent;border-bottom:4px solid rgb(137,137,199);color:black;selection-background-color:pink;}"
                                         "QLineEdit:focus{background:lightgreen;border-bottom:3px solid green;color:white;font:75;}"
                                         );

    ui->lineEdit_count->installEventFilter(this);
    //! style init.
    ui->lineEdit_count->setStyleSheet(str_lineEdit_count);
    //chkbox
    ui->ckb_ignorerror->setStyleSheet(str_ckbstyle.arg(ui->ckb_ignorerror->objectName()));
    //! End style init.

//    ui->btn_status->setAutoRaise(true);
//    ui->btn_preview->setAutoRaise(true);

    //connect
    connect(ui->lineEdit_count,&QLineEdit::editingFinished,[=]{
        emit sig_dataChange(ui->lbl_putCount->text(),(ui->lineEdit_count->text()));
        m_itemData.setCount(ui->lineEdit_count->text().toInt());
    });
    connect(ui->timeEdit_cycle,&QTimeEdit::timeChanged,[=](const QTime & time){
        emit sig_dataChange(ui->lbl_cycle->text(),(ui->timeEdit_cycle->time().toString("h:m")));
        m_itemData.setCycleTime(time);
    });
    connect(ui->timeEdit_start,&QTimeEdit::timeChanged,[=](const QTime & time){
        emit sig_dataChange(ui->lbl_putstart->text(),(ui->timeEdit_start->time().toString("h:m")));
        m_itemData.setStartTime(time);
    });

    connect(ui->btn_startPut,&QToolButton::clicked,[=]{
        emit sig_startPut();
    });

    m_itemData.setItemText(ui->lblTitleName->text() + ui->lblTitleText->text());
    m_itemData.setGroupNum(ui->lblTitleText->text());
    m_itemData.setIpadd(ui->lblvalueText->text());
    m_itemData.setStartTime(ui->timeEdit_start->time());
    m_itemData.setCycleTime(ui->timeEdit_cycle->time());
    m_itemData.setCount(ui->lineEdit_count->text().toInt());
    m_itemData.setIgnorError(ui->ckb_ignorerror->isEnabled());//-
    m_itemData.setEnable(ui->chkbox->isEnabled());//-

    //set visible to false.
    ui->lbl_cycle->setVisible(false);
    ui->lbl_putCount->setVisible(false);
    ui->lbl_putstart->setVisible(false);
    ui->lineEdit_count->setVisible(false);
    ui->timeEdit_cycle->setVisible(false);
    ui->timeEdit_start->setVisible(false);
}

FormItemWidgetGroup::~FormItemWidgetGroup()
{
    delete ui;
}

void FormItemWidgetGroup::setCheck(bool flag)
{
    ui->chkbox->setChecked(flag);
}

void FormItemWidgetGroup::setTitle(QString strname, QString strtext)
{
    ui->lblTitleName->setText(strname + QStringLiteral(":"));
    ui->lblTitleText->setText(strtext);
    m_itemData.setItemText(ui->lblTitleName->text() + ui->lblTitleText->text());
    m_itemData.setGroupNum(ui->lblTitleText->text());
}

QString FormItemWidgetGroup::getTitle()
{
    return ui->lblTitleText->text();
}

void FormItemWidgetGroup::setValue(QString strname, QString strtext)
{
    ui->lblValuename->setText(strname + QStringLiteral(":"));
    ui->lblvalueText->setText(strtext);
    m_itemData.setIpadd(ui->lblvalueText->text());
}

void FormItemWidgetGroup::setValue(QStringList strlst)
{
    setValue(strlst.first(),strlst.last());
}

void FormItemWidgetGroup::setIp(QString str)
{
    ui->lblvalueText->setText(str);
}

void FormItemWidgetGroup::updateDateTime()
{
//    ui->dly_timeEdit->setTime(QDateTime::currentDateTime().time());
}

QString FormItemWidgetGroup::getGroupNum()
{
    return ui->lblTitleText->text();
}

void FormItemWidgetGroup::setStatus(QString str)
{
    //todo->test
//    ui->lbl_status->setText(str);//should be delete.
    ui->lbl_statesSet->setText(str);
}

void FormItemWidgetGroup::updateStatus(QString str)
{
//    ui->lbl_status->setText(str);
    ui->lbl_status_opt->setText(str);
}

bool FormItemWidgetGroup::eventFilter(QObject *obj, QEvent *ev)
{
    if(obj == ui->lineEdit_count){
        if(ev->type() == QEvent::MouseButtonRelease && dynamic_cast<QMouseEvent*>(ev)->button() == Qt::LeftButton){
            ui->lineEdit_count->setCursor(Qt::IBeamCursor);
            return true;
        }

        if(ev->type() == QEvent::FocusOut){
            ui->lineEdit_count->setCursor(Qt::ArrowCursor);
            return false;
        }

//        if(ev->type() == QEvent::FocusIn){
//            qDebug("select.all");
//            ui->lineEdit_count->selectAll();
//            return false;
//        }
    }
    return QWidget::eventFilter(obj,ev);
}

void FormItemWidgetGroup::mousePressEvent()
{
    setFocus();
}
